﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using SEM.SEM.ClasesSEM;
using FAS.SEM.ClasesSEM;

namespace Arquitectura.SEM
{
    public abstract class AbstractSem : ISem
    {
        public String strInsertar,strDelete,strUpdate,id,tabla;
        protected abstract void fillSem(DataRow row);
        protected abstract void comandos();
        public abstract Dictionary<String, Object> getAtributos();
        public abstract AbstractSem get(object o);

        /********************************************************
       /* getAll
       /* Proposito: inicializar lista de objetos  existentes desde una base de datos.
       /* Parametros : Ninguna
       /* Limitaciones: Ninguna
       /* Retorna: Lista de AbstractSem
       /*********************************************************/

        public List<AbstractSem> getAll()
        {
            List<AbstractSem> abs = getAllAbstract(this.tabla, this.id, null);
            return abs;
        }

        public AbstractSem()
        {
            this.comandos();
        }

        public Boolean delete() {
            ConexionMySQLSEM conexion = new ConexionMySQLSEM();
            conexion.AccionesDB(getAtributos(),strDelete);
            return true;
        }

        public Dictionary<String, Object> getAtributosInsertar() {
            Dictionary<String, Object> lst = this.getAtributos();
            lst.Remove(lst.Last().Key);
            return lst;
        }

        public Boolean insertar() {
            ConexionMySQLSEM conexion= new ConexionMySQLSEM();
            conexion.AccionesDB(this.getAtributos(),this.strInsertar);
            return true;
        }
        public int insertarId() {
            ConexionMySQLSEM conexion = new ConexionMySQLSEM();
            int insertar = conexion.insertar(this.getAtributosInsertar(), this.strInsertar, this.id, this.tabla);
            return insertar;
        }

        public Boolean update() {
            ConexionMySQLSEM conexion = new ConexionMySQLSEM();
            conexion.AccionesDB(this.getAtributos(),this.strUpdate);
            return true;
        }

        public DataSet exec(String comando)
        {
            ConexionMySQLSEM conexion = new ConexionMySQLSEM();
            DataSet exec = conexion.GetQuery(comando);
            return exec;
        }
    
        public DataSet execProcedure(String procedure, Dictionary<String,Object> lista)
        {
            ConexionMySQLSEM conexion = new ConexionMySQLSEM();
            DataSet exec = conexion.GetQuery(procedure,lista);
            return exec;
        }
    
        public static List<AbstractSem> getAllAbstract(String tabla, String id, Object objeto)
        {
            ConexionMySQLSEM cm = new ConexionMySQLSEM();
            String query = "SELECT "+id+" FROM "+tabla;
            DataTable data = cm.GetQuery(query).Tables[0];
            List<AbstractSem> lst = new List<AbstractSem>();
            try {
                foreach(DataRow row in data.Rows)
                {
                    AbstractSem abs = getAbstractSem(row[0],tabla,id);
                    lst.Add(abs);
                }

            } catch (Exception ex) {

            }

            return lst;
        }
    
        public static AbstractSem getAbstractSemC(String tabla, String id, Object value, String id2,Object value2) 
        {    
            try{
                ConexionMySQLSEM conexion = new ConexionMySQLSEM();
                string query = "SELECT * FROM "+tabla+" where "+id+" = '" + value + "' AND "+id2+" = "+value2;
                DataSet rs = conexion.GetQuery(query);
                AbstractSem abs = null;
                
                if(tabla == "`ExamenAlumno`")
                    abs = new ExamenAlumnoSEM();
                abs.fillSem(rs.Tables[0].Rows[0]);
                return abs;
            }
            catch(Exception e){return null;}
        }
    
        public static AbstractSem getAbstractSem(Object value, String tabla, Object id) 
        {    
            try{
                ConexionMySQLSEM conexion = new ConexionMySQLSEM();
                String query = "SELECT * FROM "+tabla+" where "+id+" = '" + value + "'";
                DataSet rs = conexion.GetQuery(query);
                AbstractSem abs = null;
                if(tabla == "`Profesor`")
                    abs = new ProfesorSEM();
                else if(tabla == "`Opcion`")
                    abs = new OpcionSEM();
                else if(tabla == "`Problema`")
                    abs = new ProblemaSEM();
                if(tabla == "`ExamenAlumno`")
                    abs = new ExamenAlumnoSEM();
                abs.fillSem(rs.Tables[0].Rows[0]);
                return abs;
            }
            catch(Exception e){return null;}
        }
    
    }
}
